using System;
using System.Collections.Generic;
using System.Text;
using Mpi.Common.Collections;

namespace Xeml.Document
{
    public class SuperSample : XemlAnnotatableObject
    {
        private int _id;
        private List<Sample> _samples;

    
        private TimeIntermix _timeMode = TimeIntermix.Min;

        #region Properties

        /// <summary>
        /// The collection of samples on which this super sample is build up.
        /// </summary>
        public List<Sample> Samples
        {
            get
            {
                return _samples;
            }
        }

        /// <summary>
        /// The identifiere of the super sample.
        /// </summary>
        public int Id
        {
            get
            {
                return _id;
            }
            set
            {
                _id = value;
            }
        }

        /// <summary>
        /// A property which indicates how the time point should be calculated.
        /// </summary>
        public TimeIntermix TimeIntermix
        {
            get { return _timeMode; }
            set { _timeMode = value; }
        }

        /// <summary>
        /// The calculated time point of an sample. The calculation is done in respect to the TimeIntermix property.
        /// </summary>
        /// <seealso cref="TimeIntermix"/>
        public TimeSpan TimePoint
        {
            get
            {
                TimeSpan ret = TimeSpan.Zero;
                switch (_timeMode)
                {
                    case TimeIntermix.Min:
                        {
                            ret = TimeSpan.MaxValue;
                            foreach (Sample s in _samples)
                            {
                                ret = TimeSpan.FromTicks( Math.Min(ret.Ticks, s.TimePoint.Ticks));
                            }
                            break;
                        }
                    case TimeIntermix.Max:
                        {
                            ret = TimeSpan.Zero;
                            foreach (Sample s in _samples)
                            {
                                ret = TimeSpan.FromTicks(Math.Max(ret.Ticks, s.TimePoint.Ticks));
                            }
                            break;
                        }
                    case TimeIntermix.Average:
                        ret = TimeSpan.Zero;
                        long sum = ret.Ticks;
                        foreach (Sample s in _samples)
                        {
                            sum += s.TimePoint.Ticks;
                        }
                        sum = sum / _samples.Count;
                        ret = TimeSpan.FromTicks(sum);
                        break;
                    default:
                        goto case TimeIntermix.Min;
                }
                return ret;



            }

        }

        #endregion

        #region ctor
        public SuperSample()
        {
            _samples = new List<Sample>();
        } 
        #endregion

     
        private SuperSampleCollection _collection;
        public SuperSampleCollection Collection
        {
            get
            {
                return _collection;
            }
            set
            {
                if (value != _collection)
                {
                    _collection = value;
                }

            }
        }

      
    }
}
